<?php

namespace App\Admin\Controllers;

use App\Models\Person;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;

class PersonController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Person(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('department')->using(Person::$department_arr)->label();
            $grid->column('name');
            $grid->column('work_no');
            $grid->column('category_text', '工种');
            $grid->column('working_certificate_start_date');
            $grid->column('working_certificate_end_date');
            $grid->column('photo')->image('', 70, 120);
            $grid->column('updated_at')->sortable();
            $titles = ['id' => 'ID', 'name' => '姓名', 'work_no' => '工号', 'category_text' => '工种', 'url' => '地址'];
            $grid->export($titles)->rows(function ($rows) {
                foreach ($rows as $index => &$row) {
                    $row['url'] = url('person/' . $row['id']);
                }
                return $rows;
            })->filename('员工列表');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('name')->width(3);
            });
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->append('<a target="_blank" href="' .url('person/'. $actions->row->id .'/view'). '"><i class="fa fa-eye"></i></a>');
            });
        });
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Person(), function (Form $form) {
            $form->select('department')->options(Person::$department_arr)->required();
            $form->text('name')->required();
            $form->text('work_no')->required();
            $form->multipleSelect('category')->options(Person::$category_arr)->required();
            $form->list('station');
            $form->list('job');
            $form->dateRange('working_certificate_start_date', 'working_certificate_end_date', '上岗证时间');
            $form->dateRange('sub_working_certificate_start_date', 'sub_working_certificate_end_date', '准上岗证时间');
            $form->image('photo');

        });
    }
}
